package com.milian.jdbc.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @Author milian
 * @Date 2021/8/19 0019 16:29
 */
public class test2 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // 注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        // 获取连接
        String url = "jdbc:mysql://127.0.0.1:3306/db10?useSSL=false";
        String username="root";
        String password="root";
        Connection conn = DriverManager.getConnection(url, username, password);

        String sql1 = "update stu set age=51 where id=1";
        String sql2 = "update stu set age=51 where id=2";
        Statement stmt = conn.createStatement();
        try {
            // 开启事务
            conn.setAutoCommit(false);
            int count1 = stmt.executeUpdate(sql1);
            int count2 = stmt.executeUpdate(sql2);
            System.out.println(count1);
            System.out.println(count2);
            conn.commit();
        } catch (SQLException throwables) {
            conn.rollback();
            throwables.printStackTrace();
        }
        stmt.close();
        conn.close();


    }
}
